package com.tnovoselec.android.feed.handler.resultholders;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.tnovoselec.android.favorites.entity.NewsFavoritesDto;


public class NewsResultHolder {

    private static Map<String, List<NewsFavoritesDto>> MAP_RESULT = new HashMap<String, List<NewsFavoritesDto>>();

    private NewsResultHolder() {
	super();
    }

    public static void init(String url, List<NewsFavoritesDto> items) {
	MAP_RESULT.put(url, items);
    }

    public static void clear() {
	NewsResultHolder.MAP_RESULT.clear();
    }

    public static List<NewsFavoritesDto> getNewsByChannelId(String channelId) {
	return MAP_RESULT.get(channelId);
    }

    public static NewsFavoritesDto getNewsByGuid(String guid) {
	for (Entry<String, List<NewsFavoritesDto>> entry : MAP_RESULT.entrySet()) {
	    for (NewsFavoritesDto news : entry.getValue()) {
		if (news.getGuid().equals(guid)) {
		    return news;
		}
	    }
	}
	return null;
    }

    public static int getNewsPositionByGuid(String guid) {
	for (Entry<String, List<NewsFavoritesDto>> entry : MAP_RESULT.entrySet()) {
	    final List<NewsFavoritesDto> newsList = entry.getValue();
	    for (int i = 0; i < newsList.size(); i++) {
		if (newsList.get(i).getGuid().equals(guid)) {
		    return i;
		}
	    }
	}
	return -1;
    }

}
